package com.weakteam.orderonline.adminconsole.gwt.client.widget;

import com.allen_sauer.gwt.dnd.client.PickupDragController;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.DoubleClickEvent;
import com.google.gwt.event.dom.client.DoubleClickHandler;
import com.google.gwt.event.logical.shared.*;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.json.client.JSONArray;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.user.client.ui.AbsolutePanel;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Widget;
import com.weakteam.orderonline.adminconsole.gwt.client.domain.Goods;
import com.weakteam.orderonline.adminconsole.gwt.client.util.Responser;
import com.weakteam.orderonline.adminconsole.gwt.client.util.Requester;

/**
 * Created by Max on 30.03.2014.
 */
public class MenuGoodsContainer extends FlowPanel implements
        Responser, HasSelectionHandlers<MenuGoodsBox> {

    private PickupDragController widgetDragController;
    private MenuGoodsBox selectedBox;
    private int category;

    public MenuGoodsContainer(AbsolutePanel dragSpace) {
        dragSpace.add(this);
        this.addStyleName("content-container");
        // initialize a widget drag controller
        this.widgetDragController = new PickupDragController(dragSpace, false);
        this.widgetDragController.setBehaviorMultipleSelection(false);
        // initialize a widget drop controller
        MenuGoodsDropController menuGoodsDropController = new MenuGoodsDropController(this);
        // register drop controller
        this.widgetDragController.registerDropController(menuGoodsDropController);
        this.widgetDragController.setBehaviorDragStartSensitivity(3);
    }

    @Override
    public void handleGetResponse(JSONArray data) {
        this.clear();
        this.selectedBox = null;
        int n = data.size();
        if (n > 0) {
            for(int i = 0; i < n; i++) {
                JSONObject obj = data.get(i).isObject();
                final Goods goods = new Goods(obj);
                addBox(new MenuGoodsBox (goods));
            }
        }
    }
    @Override
    public void handlePostResponse(int responseCode) {
        Requester request = new Requester();
        request.addResponser(MenuGoodsContainer.this);
        request.getGoodsAll(this.category);
    }
    @Override
    public HandlerRegistration addSelectionHandler(SelectionHandler<MenuGoodsBox> handler) {
        return super.addHandler(handler, SelectionEvent.getType());
    }


    protected void setCategory (int category) {
        this.category = category;
    }

    protected void setBoxSelected(MenuGoodsBox newSelectedBox) {
        for (Widget box : this.getChildren()) {
            if (box instanceof MenuGoodsBox) {
                MenuGoodsBox mgBox = (MenuGoodsBox)box;
                if (mgBox.isSelected()) {
                    mgBox.setSelected(false);
                }
            }
        }
        (this.selectedBox = newSelectedBox).setSelected(true);
        SelectionEvent.fire(this,selectedBox);
    }

    protected void saveData() {
        JSONArray data = new JSONArray();
        int i = 0;
        for (Widget box : this.getChildren()) {
            if (box instanceof MenuGoodsBox) {
                MenuGoodsBox mgBox = (MenuGoodsBox) box;
                mgBox.setGoodsOrder(this.getWidgetIndex(box));
                data.set(i++, mgBox.getGoods().getJSON());
            }
        }
        new Requester().setGoodsAll(data);
    }

    public void addNewItem() {
        Goods newGoods = new Goods();
        newGoods.setMenuId(this.category);
        MenuGoodsPopup popup = new MenuGoodsPopup(newGoods);
        popup.addValueChangeHandler(new ValueChangeHandler<Goods>() {
            @Override
            public void onValueChange(ValueChangeEvent<Goods> event) {
                Goods newValue = event.getValue();
                Requester request = new Requester();
                request.addResponser(MenuGoodsContainer.this);
                request.setGoodsOne(newValue.getJSON());
            }
        });
    }

    public void editSelectedItem() {
        if (selectedBox != null) {
            editBox(selectedBox);
        }
    }

    public void deleteSelectedItem() {
        if (selectedBox != null) {
            Requester request = new Requester();
            request.addResponser(MenuGoodsContainer.this);
            request.deleteGoodsById(selectedBox.getGoodsId());
        }
    }

    private void addBox(final MenuGoodsBox box) {
        this.add(box);
        box.addClickHandler(new ClickHandler() {
            @Override
            public void onClick(ClickEvent event) {
                setBoxSelected(box);
            }
        });
        box.addDoubleClickHandler(new DoubleClickHandler() {
            @Override
            public void onDoubleClick(DoubleClickEvent event) {
                editBox(box);
            }
        });
        // make the widget draggable
        this.widgetDragController.makeDraggable(box);
    }

    public void editBox(final MenuGoodsBox box) {
        MenuGoodsPopup popup = new MenuGoodsPopup(box.getGoods());
        popup.addValueChangeHandler(new ValueChangeHandler<Goods>() {
            @Override
            public void onValueChange(ValueChangeEvent<Goods> event) {
                Goods newValue = event.getValue();
                new Requester().setGoodsOne(newValue.getJSON());
                box.setData(newValue);
            }
        });
    }

}
